--[[ Lambda InfoUnits
     Part of LambdaUI made by Gaius
	 Graphics are made by Gaius
	 ------------------------------
	 Contact me in irc://phcnet.de/#wow-interface
--]]

local filePath = "Interface\\AddOns\\LambdaUI\\"
local genericFilePath = "Interface\\AddOns\\LambdaUI\\Generic\\"

--[[  Lambda Info Class

--]]
local Lambda = {}

function Lambda:new(Name)
	local Lambda = {}
    local filePath = filePath.."Info\\"
	
    Lambda = CreateFrame("Button", "LambdaInfo"..Name, UIParent)
   	
	Lambda.BackgroundTexture = Lambda:CreateTexture("LambdaInfo"..Name.."Border","BACKGROUND")
	
	Lambda:SetWidth(64)
	Lambda:SetHeight(16)
	
	Lambda.BackgroundTexture:SetTexture(filePath.."Background.tga")
	Lambda.BackgroundTexture:SetAllPoints(Lambda)
	
	Lambda:SetPushedTextOffset(0,0)
	
	Lambda:SetFont(genericFilePath.."bold.ttf",10)
	
	Lambda:RegisterEvent("PLAYER_ENTERING_WORLD")
	Lambda:RegisterForClicks("AnyDown")
	table.insert(LambdaObjectTable,Lambda:GetName())
	
	Lambda.UpdateFontColors = function()
		Lambda:SetTextColor(unpack(LambdaUI.TextColors.BorderFont))
	end
	
	Lambda:SetFrameLevel(2)
	return Lambda
end 

local expInfo = Lambda:new("Experience")
expInfo:SetScript("OnEvent", function()
	if UnitLevel("player") ~= MAX_PLAYER_LEVEL then
		expInfo:SetText(math.abs(UnitXP("player") - UnitXPMax("player")))
	else
		expInfo:SetText("open log")
	end
end)

expInfo:SetScript("OnClick", function()
	if QuestLogFrame:IsShown() then
		QuestLogFrame:Hide()
	else
		QuestLogFrame:Show()
	end
end)

expInfo:SetScript("OnEnter", function() -- make hover effect
	expInfo:SetText("open log")
end)

expInfo:SetScript("OnLeave", function() -- make hover effect
	if UnitLevel("player") ~= MAX_PLAYER_LEVEL then
		expInfo:SetText(math.abs(UnitXP("player") - UnitXPMax("player")))
	else
		expInfo:SetText("open log")
	end
end)


expInfo:RegisterEvent("PLAYER_XP_UPDATE")
expInfo:RegisterEvent("PLAYER_LEVEL_UP")
expInfo:SetPoint("TOPLEFT",UIParent,"BOTTOMLEFT",179, 180)

local mailInfo = Lambda:new("Mail")
mailInfo:SetScript("OnEvent", function()
	if HasNewMail() then
	mailInfo:SetText("new mail")
	else
	mailInfo:SetText("no mail")
	end
end)

MiniMapMailFrame:Hide()
MiniMapMailFrame.Show = function() end

mailInfo:RegisterEvent("UPDATE_PENDING_MAIL")
mailInfo:SetPoint("TOPLEFT",UIParent,"BOTTOMLEFT",174, 164)

local timeInfo = Lambda:new("Time")
timeInfo:SetScript("OnUpdate", function()
	if GetMouseFocus() ~= timeInfo then
		timeInfo:SetText(date("%H:%M:%S"))
	end
end)

timeInfo:SetScript("OnEnter", function()
	if GroupCalendar then
		timeInfo:SetText("calendar")
	end
end)

timeInfo:SetScript("OnLeave", function()
	timeInfo:SetText(date("%H:%M:%S"))
end)

timeInfo:SetScript("OnClick", function()
	if GroupCalendar then -- check if GroupCalendar is loaded
		if GroupCalendarFrame:IsShown() then
			GroupCalendarFrame:Hide()
		else
			GroupCalendarFrame:Show()
		end
	end
end)

timeInfo:SetPoint("TOPLEFT",UIParent,"BOTTOMLEFT",164, 148) 

local BGInfo = Lambda:new("Battlefield")
BGInfo:SetScript("OnEvent", function()
	if GetBattlefieldStatus(1) ~= "none" or GetBattlefieldStatus(2) ~= "none" or GetBattlefieldStatus(3) ~= "none" then
		if not BGInfo:IsShown() then 
			BGInfo:Show()
		end
		if GetBattlefieldStatus(1) == "active" or GetBattlefieldStatus(2) == "active" or GetBattlefieldStatus(3) == "active" then
			BGInfo:SetText("active")
		elseif GetBattlefieldStatus(1) == "queued" or GetBattlefieldStatus(2) == "queued" or GetBattlefieldStatus(3) == "queued" then
			BGInfo:SetText("queued")
		elseif GetBattlefieldStatus(1) == "confirm" or GetBattlefieldStatus(2) == "confirm" or GetBattlefieldStatus(3) == "confirm" then
			BGInfo:SetText("confirm")
		end
	else
		BGInfo:Hide()
	end
end)

BGInfo:SetScript("OnClick", function()
	if arg1 == "RightButton" then
		ToggleDropDownMenu(1, nil, MiniMapBattlefieldDropDown, "LambdaInfoBattlefield", 0, -5);
	elseif IsShiftKeyDown() then
			ToggleBattlefieldMinimap();
	else
			ToggleWorldStateScoreFrame();
	end
end)

MiniMapBattlefieldFrame:Hide()
MiniMapBattlefieldFrame.Show = function() end

BGInfo:Hide()
BGInfo:RegisterEvent("UPDATE_BATTLEFIELD_STATUS")
BGInfo:UnregisterEvent("PLAYER_ENTERING_WORLD")

BGInfo:SetPoint("TOPLEFT",UIParent,"BOTTOMLEFT",150, 132)

local BagInfo = Lambda:new("Bag")
BagInfo:SetScript("OnEvent", function()
	BagInfo.counter=0
	BagInfo.maximum=1
	for bag=0,4 do
		local bagName = GetBagName(bag)
		if bagName then
			local itemType = select(6, GetItemInfo(bagName))
			if itemType == LambdaLocale.Bag[GetLocale()] or bag == 0 then -- do not count special bags
			
				for slot=0,GetContainerNumSlots(bag) do
					BagInfo.maximum = BagInfo.maximum + 1
					if GetContainerItemInfo(bag,slot) == nil then -- no item here
						BagInfo.counter = BagInfo.counter +1
					end

				end
			end
		end
	end
	BagInfo:SetText(BagInfo.counter.."/"..BagInfo.maximum)
end)

BagInfo:SetScript("OnClick", function()
	for i=0,4 do
		ToggleBag(i)
	end
end)

BagInfo:SetScript("OnEnter", function()
	BagInfo:SetText("open Bags")
end)

BagInfo:SetScript("OnLeave", function()
	BagInfo.counter=0
	BagInfo.maximum=1
	for bag=0,4 do
		if GetBagName(bag) then
			local bagName = GetBagName(bag)
			local itemType = select(6, GetItemInfo(bagName))
			if itemType == "bag" or itemType == "Beh\195\164lter" or bagName == "Rucksack" or bagName == "Backpack" then -- do not count special bags
			
				for slot=0,GetContainerNumSlots(bag) do
					BagInfo.maximum = BagInfo.maximum + 1
					if GetContainerItemInfo(bag,slot) == nil then -- no item here
						BagInfo.counter = BagInfo.counter +1
					end

				end
			end
		end
	end
	BagInfo:SetText(BagInfo.counter.."/"..BagInfo.maximum)
end)

BagInfo:RegisterEvent("BAG_UPDATE")
BagInfo:RegisterEvent("PLAYER_ENTERING_WORLD")

BagInfo:SetPoint("TOPLEFT",UIParent,"BOTTOMLEFT", 182, 196)